package com.example.eshop_ssm.service.impl;

import com.example.eshop_ssm.entity.TbOrderInfo;
import com.example.eshop_ssm.mapper.OrderManagerMapper;
import com.example.eshop_ssm.service.OrderManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderManagerServiceImpl implements OrderManagerService {
    @Autowired
    private OrderManagerMapper orderManagerMapper;

    @Override
    public HashMap<String, Object> selectOrders(Integer page, String orderId, String person, String orderDate, String status) {
        HashMap<String, Object> returnOrdersMap = new HashMap<>();//返回的map
        HashMap<String, Object> conditionMap = new HashMap<>();//查询条件的map

        //计算对应页从哪条记录开始
        if (page == null || page == 0) {
            page = 1;
        } else {
            page = (page - 1) * 10;  //每页10条数据
        }
        conditionMap.put("page", page);
        conditionMap.put("orderId", orderId);
        conditionMap.put("person", person);
        conditionMap.put("orderDate", orderDate);

        if (status != null && !("").equals(status)) {
            String column = status.substring(0, status.lastIndexOf("_"));
            String val = status.substring(status.lastIndexOf("_") + 1, status.length());
            conditionMap.put("column", column);
            conditionMap.put("val", val);
        } else {
            conditionMap.put("column", "");
        }

        //获取总记录数
        Integer integer = orderManagerMapper.ordersCount(conditionMap);
        returnOrdersMap.put("totalPage", integer % 10 == 0 ? integer / 10 : integer / 10 + 1);

        List<TbOrderInfo> tbOrderInfos = orderManagerMapper.selectOrders(conditionMap);//查询订单信息
        String orderStatus = "";//订单状态
        for (TbOrderInfo order : tbOrderInfos) {
            //订单正常未取消
            if (order.getStatus() == 1) {
                //"已付款";
                if (order.getStatusPay() == 1) {
                    //"已发货";
                    if (order.getStatusSend() == 1) {
                        //已收货
                        if (order.getStatusReceiving() == 1) {
                            orderStatus = "已收货";
                        } else {
                            orderStatus = "未收货";
                        }
                    } else {
                        orderStatus = "未发货";
                    }
                } else {
                    orderStatus = "未付款";
                }
            } else {
                orderStatus = "已取消";
            }
            order.setOrderStatus(orderStatus);
        }

        returnOrdersMap.put("orders", tbOrderInfos);
        return returnOrdersMap;
    }


    @Override
    public Integer deleteOrders(List idList) {
        return orderManagerMapper.deleteOrders((String[]) idList.toArray());
    }

    public Integer canclOrDelete(String control, String orderIds){
        if ("delete".equals(control)){
          return  orderManagerMapper.deleteOrders(orderIds.split(","));
        }else if ("cancl".equals(control)){
            HashMap<Object, Object> canclMao = new HashMap<>();
            String[] ids = orderIds.split(",");
            return orderManagerMapper.cancal(ids);
        }
        return 0;
    }


    public TbOrderInfo orderDetail(String id){
        return orderManagerMapper.orderDetail(id);
    }

    @Override
    public List<Map<String, Object>> selectGoods(String id) {
        return orderManagerMapper.selectGoods(id);
    }

    @Override
    public List<Map<String, Object>> selectOrderUserDetail(String id) {
        return orderManagerMapper.selectOrderUserDetail(id);
    }
}
